package com.tsingtao.springboot2.api.controller;

import cn.hutool.core.collection.CollUtil;
import com.tsingtao.springboot2.dto.Company;
import com.tsingtao.springboot2.service.CompanyService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.compiler.CompileError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * The type Company controller.
 */
@Slf4j
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    /**
     * Select all list.
     *
     * @return the list
     */
    @GetMapping("/select-all")
    public List<Company> selectAll() {
        return companyService.selectAll();
    }

    /**
     * Select by code list.
     *
     * @param code the code
     * @return the list
     */
    @GetMapping("/select-by-code")
    public List<Company> selectByCode(@RequestParam(required = false) String code) {
        return companyService.selectByCode(code);
    }

    @GetMapping("/select-by-choose")
    public List<Company> selectByChoose(@RequestBody Company company) {
        return companyService.selectByChoose(company);
    }

    @GetMapping("/select-by-code-or-name")
    public List<Company> selectByCodeOrName(@RequestParam(required = false) String code, @RequestParam(required = false) String name) {
        return companyService.selectByCodeOrName(code, name);
    }

    @GetMapping("/select-all-by-proxy")
    public List<Company> selectAllByProxy() {
        return companyService.selectAllByProxy();
    }

    /**
     * Insert company.
     *
     * @param company the company
     * @return the company
     */
    @PostMapping("/insert")
    public Company insert(@RequestBody Company company) {
        return companyService.insert(company);
    }


    /**
     * 批量插入
     * Postman调用是，需要传递json数组
     *
     * @param companies the companies
     * @return the list
     */
    @PostMapping("/batch-insert")
    public List<Company> batchInsert(@RequestBody List<Company> companies) {
        return companyService.batchInsert(companies);
    }

    @PostMapping("/batch-insert-by-foreach")
    public List<Company> batchInsertByForeach(@RequestBody List<Company> companies) {
        return companyService.batchInsertByForeach(companies);
    }

    /**
     * 更新
     *
     * @param company the company
     * @return the company
     */
    @PutMapping("/update")
    public Company update(@RequestBody Company company) {
        return companyService.update(company);
    }

    /**
     * 删除
     *
     * @param id the id
     * @return the int
     */
    @DeleteMapping("/delete-by-id/{id}")
    public int deleteById(@PathVariable(name = "id") Long id) {
        return companyService.deleteById(id);
    }

    @DeleteMapping("/batch-delete-by-ids")
    public int batchDeleteByIds(@RequestBody List<Company> companies) {
        log.info("需要批量删除的数据：{}", companies);
        if(CollUtil.isEmpty(companies)) {
            throw new RuntimeException("请传递数据！！！");
        }
        return companyService.batchDeleteByIds(companies);
    }
}
